﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADCCore;
using ADCCore.Classes;
using ADCCore.Implementation;
namespace Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            dcClient.ClientInfo.HubAdress = txtHub.Text;
            dcClient.ClientInfo.Username = txtNick.Text;
            dcClient.ClientInfo.Description = txtDesc.Text;
            dcClient.ClientInfo.Password = txtPass.Text;
            dcClient.ClientInfo.PID = "Y7CW4TOHL4AJQKMCGTGGAFPSMR3PUSBXA6BKWEQ";            
            dcClient.Connect();
        }

        private string RunCommands(string text)
        {
            //Read the first word of the text and check for trigger
            String[] aText = text.Split(new Char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
            switch (aText[0].ToLower())
            {
                case "+test":
                    //Change spaces to char + for compatibility
                case "+help":
                    return "Showing Help:\n"
                        + "+random shows a random quote\n"
                        + "+latest shows one of the most resent quotes\n"
                        + "+top shows one of the most rated quotes\n"
                        + "+quote [id] shows quote with specified id\n"
                        + "+search [words] shows a quote matching your search";
            }
            return "";
        }

        private void dcClient_OnMainchatMessage(DcClient sender, User user, string text, bool ownMessage)
        {
            String msg = String.Format("<{0}> {1}\n", user.Username, text);
            msg = msg.Replace("\n", Environment.NewLine);
            txtMainChat.AppendText(msg);
            String[] cmd = text.Split(" ".ToCharArray());
            switch (cmd[0].ToLower())
            {
                case "+rel":
                case "+news":
                case "+next":
                case "+prev":
                    dcClient.SendMainchatMessage(RandomizeMessage());
                    break;
            }
        }

        private string RandomizeMessage()
        {
            String[] insults = {"Oh really, won't happen.",
                               "I have no idea what you are taling about.",
                               "If I got a quarter for everytime someone said that to me.",
                               "I'm on strike",
                               "Attica! Attica! Attica! Attica! Attica!",
                               "Stop, please i need some rest",
                               "You know, you are like the energizer bunny, just keeps going and going...",
                               "Read my letters N O",
                               "42",
                               "That's the most ridiculous thing I've ever heard.",
                               "I see dumb people, yeah I'm looking at you!",
                               "You don't think i got more things to say? Well you might be right."
                               };
            Random _random = new Random();
            int id = _random.Next(0, insults.Length-1);
            return insults[id];
        }

        private string RandomizePMMessage()
        {
            String[] insults = {"Oh really, won't happen.",
                               "I have no idea what you are taling about.",
                               "If I got a quarter for everytime someone said that to me.",
                               "I'm on strike",
                               "Attica! Attica! Attica! Attica! Attica!",
                               "Stop, please i need some rest",
                               "You know, you are like the energizer bunny, just keeps going and going...",
                               "Read my letters N O",
                               "42",
                               "That's the most ridiculous thing I've ever heard.",
                               "I see dumb people, yeah I'm looking at you!",
                               "You don't think i got more things to say? Well you might be right.",
                               "Stop PMing me you perv!",
                               "No I won't be your joystick"
                               };
            Random _random = new Random();
            int id = _random.Next(0, insults.Length - 1);
            return insults[id];
        }

        private void dcClient_OnJoin(DcClient sender, User user)
        {
            lstUsers.Items.Add(user.Username);
        }

        private void dcClient_OnParts(DcClient sender, User user)
        {
            lstUsers.Items.Remove(user.Username);
        }

        private void dcClient_OnTopic(DcClient sender, string text)
        {
            this.Text = text;
        }

        private void txtChat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                dcClient.SendMainchatMessage(txtChat.Text);
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            dcClient.Disconnect();
            lstUsers.Items.Clear();
            txtChat.Clear();
        }

        private void dcClient_OnPrivateMessageRecieved(DcClient sender, User user, string text)
        {
                dcClient.SendPrivateMessage(user, RandomizePMMessage());
        }

        private void dcClient_OnDisconnected(DcClient sender)
        {
            lstUsers.Items.Clear();
            txtChat.Clear();
            this.Text = "Form1";
        }

        private void dcClient_OnStatusMessageRecived(DcClient sender, int StatusCode, string Message)
        {
            txtMainChat.AppendText("Status message: " + Message + "\n");
        }

        private void dcClient_OnMessageReceived(DcClient sender, string text)
        {
            txtMainChat.AppendText("RawCommand Recived: " + text + "\n");
        }

        private void dcClient_OnSearch(DcClient sender, SearchInfo searchInfo)
        {
            txtMainChat.AppendText("Incomming Search: " + searchInfo.SearchPattern + "\n");
        }

        private void dcClient_OnSearchReply(DcClient sender, SearchReply searchReply)
        {
            String sr = String.Format("SearchReplay: token={0}, Filename: {1}, Username:{2}, FreeSlots: {3}",searchReply.Token,searchReply.FilePath, searchReply.User.Username, searchReply.User.OpenSlots);
            txtMainChat.AppendText(sr + "\n");
        }
    }
}
